<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class AnswerModel extends Model
{


     /**
     * 通过审核
     *
     * @access public
     * @param $ansID array
     * @return null
    */
	public function approveAnswerByID($ansID)
	{
		$where = array('answer_id' => array('in', $ansID));
		$this->where($where)->setField('is_visible', 1);
	}


	/**
     * 否决审核
     *
     * @access public
     * @param $ansID array
     * @return null
    */
	public function disapproveAnswerByID($ansID)
	{
		$where = array('answer_id' => array('in', $ansID));
		$this->where($where)->setField('is_visible', -1);
	}


	/**
     * 还原答案
     *
     * @access public
     * @param $ansID array
     * @return null
    */
	public function restoreAnswerByID($ansID)
	{
		$where = array('answer_id' => array('in', $ansID));
		$this->where($where)->setField('is_visible', 1);
	}


	/**
     * 删除答案
     * @access public
     * @param array $ansID
     * @param int $direct 直接删除
     * @return null
     */
	public function deleteAnswerByID($ansID, $direct=0)
	{
		$recycle = get_site_config('recycle_bin_enable');
		$where = array('answer_id' => array('in', $ansID));
		if ($direct || !$recycle)
		{
			//删除团队编辑版本记录
			$AnswerVersion = M('AnswerVersion');
			$AnswerVersion->where($where)->delete();
			
			$this->where($where)->delete();
		}
		else
		{
			$this->where($where)->setField('is_visible', -1);
		}
	}


	/**
	 * 更新答案评论计数
	 *
	 * @access public
	 * @param int $answerID
	 * @return null
	 */
	public function updateCommentCount($answerID)
	{
	    $Comment = M('Comment');
	    $map = array('related_id' => $answerID, 'type' => 'A', 'is_visible' => 1);
		$count = $Comment->where($map)->count();

		$where = array('answer_id' => $answerID);
		$this->where($where)->setField('comment_count', intval($count));
	}
	
	
	/**
	 * 更新答案编辑人数计数
	 *
	 * @access public
	 * @param int $answerID
	 * @return null
	 */
	public function updateEditorCount($answerID)
	{
	    $AnswerVersion = M('AnswerVersion');
	    $map = array('answer_id' => $answerID);
		$count = $AnswerVersion->where($map)->group('editor_id')->count();

		$where = array('answer_id' => $answerID);
		$this->where($where)->setField('editor_count', intval($count));
	}

}


/* End of file AnswerModel.class.php */
/* Location: ./App/Lib/Model/AnswerModel.class.php */